home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9209 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.8 KB  |  75 lines

  1. Path: news1.cle.ab.com!usenet
  2. From: don.phillips@ab.com (Donald-Anthony C. Phillips)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: read/write integers to files
  5. Date: Fri, 08 Mar 1996 22:33:29 GMT
  6. Organization: The Allen-Bradley Co., Inc
  7. Distribution: inet
  8. Message-ID: <4hq1t6$40d@news1.cle.ab.com>
  9. References: <313EBF65.4E82@www.inia.net.au>
  10. NNTP-Posting-Host: abpc386.cle.ab.com
  11. X-Newsreader: Forte Free Agent 1.0.82
  12.  
  13. Jason Collins <jason@www.inia.net.au> wrote:
  14.  
  15. >Hi there,
  16.  
  17. >I would preface this question with the standard "dumb newbie question......." or whatever but I 
  18. >think you'll work it out for yourselves soon enough.
  19.  
  20. >My problem is that I'm trying to write a program that will read an integer from the file 
  21. >count.dat, increment the integer then write it back to count.dat.  I have provided the listing so 
  22. >that you can all tell me what I'm doing wrong.
  23.  
  24. >Thanks....and.....be gentle.
  25.  
  26. >--------------------
  27. >counter.c
  28. >--------------------
  29.  
  30. >#include <stdio.h>
  31. >#include <stdlib.h>
  32.  
  33. >FILE *filePtr;
  34.  
  35. >void main()
  36. >{
  37. >  char ctr;
  38.  
  39. >  filePtr=fopen("count.dat", "ab");
  40. >  fscanf(filePtr, "%d", &ctr);
  41. >  fclose(filePtr);
  42.  
  43. >  ctr++;
  44.  
  45. >  filePtr=fopen("count.dat", "wb");
  46. >  fprintf(filePtr, "%d", ctr);
  47. >  fclose(filePtr);
  48. >}
  49. ..........................................
  50. Jason,
  51.     Here are a few observations of your source code.  You may aslo want to
  52. post any error messages you have.
  53.  
  54. Peolpe get real testy about Mains declaration:
  55.  
  56.     int main(void)
  57.  
  58. Be sure to check the results of your fopen:
  59.  
  60.     if( (filePtr = fopen("count.dat","wb") ) == NULL) {
  61.         printf("error opening file/n");
  62.         exit(1);
  63.     }
  64.  
  65. You seem to be on the right track.
  66.  
  67.  
  68. Donald-Anthony C. Phillips
  69. Programmer/Analyst
  70.  Rockwell  Automation
  71.        ---------------------------------------
  72.         Allen-Bradley
  73. don.phillips@ab.com
  74.  
  75.